//
//  XGCalendarCell.m
//  SACalendar
//
//  Created by YaoZhaofeng on 15/4/16.
//  Copyright (c) 2015年 SyncoApp. All rights reserved.
//

#import "XGCalendarCell.h"
#import "XGCalendarConstants.h"

@interface XGCalendarCell ()
// Background colors
@property (nonatomic, strong) UIColor *normalBackgroundColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *selectedBackgroundColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *inactiveSelectedBackgroundColor UI_APPEARANCE_SELECTOR;

//  Overrides normalBackgroundColor and selectedBackgroundColor for cell representing today
@property (nonatomic, strong) UIColor *todayBackgroundColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *todaySelectedBackgroundColor UI_APPEARANCE_SELECTOR;

//  Text colors for today override the default text colors
@property (nonatomic, strong) UIColor *todayTextColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *todayTextShadowColor UI_APPEARANCE_SELECTOR;

// Text color and shadow color
@property (nonatomic, strong) UIColor *textColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *textShadowColor UI_APPEARANCE_SELECTOR;

// Selected text color and shadow color
@property (nonatomic, strong) UIColor *textSelectedColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *textSelectedShadowColor UI_APPEARANCE_SELECTOR;

//weekend text color
@property (nonatomic, strong) UIColor *textColorInWeedend UI_APPEARANCE_SELECTOR;

// Color for the event dot
@property (nonatomic, strong) UIColor *dotColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *selectedDotColor UI_APPEARANCE_SELECTOR;

// Border colors
@property (nonatomic, strong) UIColor *cellBorderColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *selectedCellBorderColor UI_APPEARANCE_SELECTOR;

@property (nonatomic, strong) UIView *dot;
@end

@implementation XGCalendarCell
/**
 *  Draw the basic components of the cell, including the top grey line, the red current date circle,
 *  the black selected circle and the date label. Customized the cell apperance by editing this function.
 *
 *  @param frame - size of the cell
 *
 *  @return initialized cell
 */
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _state = XGCalendarCellStateNormal;
        //  Normal Cell Colors
        _normalBackgroundColor = kCalendarColorClearColor;
        _selectedBackgroundColor = kCalendarColorBlue;
        _inactiveSelectedBackgroundColor = kCalendarColorDarkGray;
        
        //  Today Cell Colors
        _todayBackgroundColor = kCalendarColorClearColor;
        _todaySelectedBackgroundColor = kCalendarColorBlue;
        _todayTextShadowColor = kCalendarColorTodayShadowBlue;
        _todayTextColor = kCalendarColorBlue;
        
        //  Text Colors
        _textColor = kCalendarColorDarkTextGradient;
        _textShadowColor = [UIColor whiteColor];
        _textSelectedColor = [UIColor whiteColor];
        _textSelectedShadowColor = kCalendarColorSelectedShadowBlue;
        _textColorInWeedend = kCalendarColorWeekend;
        
        _dotColor = kCalendarColorBlue;
        _selectedDotColor = kCalendarColorBlue;
        
        _cellBorderColor = kCalendarColorCellBorder;
        _selectedCellBorderColor = kCalendarColorSelectedCellBorder;
        
        [self setBackgroundColor:[self normalBackgroundColor]];
        
        self.topLineView = [[UIView alloc]initWithFrame:CGRectMake(-1, 0, frame.size.width + 2, .5f)];
        self.topLineView.backgroundColor = cellTopLineColor;
        self.bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(-1, frame.size.height-1, frame.size.width + 2, 1)];
        self.bottomLineView.backgroundColor = cellTopLineColor;
        
        self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height * labelToCellRatio)];
        self.dateLabel.backgroundColor = [UIColor clearColor];
        self.dateLabel.textAlignment = NSTextAlignmentCenter;
        
        CGRect labelFrame = self.dateLabel.frame;
        CGSize labelSize = labelFrame.size;
        
        CGPoint origin;
        int length;
        if (labelSize.width > labelSize.height) {
            origin.x = (labelSize.width - labelSize.height * circleToCellRatio) / 2;
            origin.y = (labelSize.height * (1 - circleToCellRatio)) / 2;
            length = labelSize.height * circleToCellRatio;
        }
        else{
            origin.x = (labelSize.width * (1 - circleToCellRatio)) / 2;
            origin.y = (labelSize.height - labelSize.width * circleToCellRatio) / 2;
            length = labelSize.width * circleToCellRatio;
        }
        
        self.circleView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, length, length)];
        
        self.circleView.layer.cornerRadius = length / 2;
        self.circleView.backgroundColor = [self selectedBackgroundColor];
        
        self.selectedView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, length, length)];
        
        self.selectedView.layer.cornerRadius = length / 2;
        self.selectedView.backgroundColor = [self selectedBackgroundColor];
        
        _dot = [UIView new];
        [self configureDot];
        
        [self.viewForBaselineLayout addSubview:self.topLineView];
        [self.viewForBaselineLayout addSubview:self.circleView];
        [self.viewForBaselineLayout addSubview:self.selectedView];
        [self.viewForBaselineLayout addSubview:self.dateLabel];
        [self.viewForBaselineLayout addSubview:self.dot];
        [self.viewForBaselineLayout addSubview:self.bottomLineView];
        
    }
    return self;
}

#pragma mark - Dot

- (void)configureDot
{
    UIView *dot = [self dot];
    
    //    CGFloat dotRadius = 3;
    CGFloat dotRadius = 8;
    CGFloat selfWidth = [self frame].size.width;
    
    [[dot layer] setCornerRadius:dotRadius/2];
    
    CGRect dotFrame = CGRectMake(selfWidth/2 - dotRadius/2, CGRectGetMaxY(self.circleView.frame) + 2, dotRadius, dotRadius);
    [[self dot] setFrame:dotFrame];
    [[self dot] setBackgroundColor:[self dotColor]];
    [self.dot setHidden:YES];
}

- (void)setState:(XGCalendarCellState)state{
    _state = state;
    
    [self.topLineView setAlpha:1.0f];
    [self.bottomLineView setAlpha:0.0f];
    [self.dateLabel setAlpha:1.0f];
    [self.selectedView setAlpha:0.0f];
    [self.circleView setAlpha:0.0f];
    switch (state) {
        case XGCalendarCellStateTodaySelected:
        {
            [self.dateLabel setTextColor:[self textSelectedColor]];
            [self.selectedView setAlpha:1.0f];
        }
            break;
        case XGCalendarCellStateTodayDeselected:
        {
            [self.dateLabel setTextColor:[self todayTextColor]];
        }
            break;
            
        case XGCalendarCellStateNormal:
        {
            [self.dateLabel setTextColor:[self textColor]];
        }
            break;
            
        case XGCalendarCellStateSelected:
        {
            [self.dateLabel setTextColor:[self textSelectedColor]];
            [self.selectedView setAlpha:1.0f];
        }
            break;
            
        case XGCalendarCellStateWeekend:
        {
            [self.dateLabel setTextColor:[self textColorInWeedend]];
        }
            break;
            
        case XGCalendarCellStateInactive:
        {
            [self.dateLabel setAlpha:0.0f];
        }
            break;
        default:
            break;
    }
    [[self dot] setAlpha:[[self dateLabel] alpha]];
}

- (void)setNumber:(NSNumber *)number
{
    _number = number;
    //  TODO: Locale support?
    if ([number intValue] == 100) {
        NSString *stringVal = @"今天"; //LLLocalizedString(@"today");
        [[self dateLabel] setText:stringVal];
//        [[self dateLabel] setFont:GetFont(16.0)];
    }else{
        NSString *stringVal = [number stringValue];
        [[self dateLabel] setText:stringVal];
//        [[self dateLabel] setFont:GetFont(20.0)];
    }
}

- (void)showBottomLine{
    [self.topLineView setAlpha:0.0f];
    [self.bottomLineView setAlpha:1.0f];
}

- (void)showDot:(BOOL)showDot{
    [self.dot setHidden:!showDot];
}

@end
